package com.example.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.entity.member.MemberReceiveAddressEntity;
import com.example.common.to.ware.FareTo;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;
import com.example.common.utils.R;
import com.example.gulimall.ware.dao.WareInfoDao;
import com.example.gulimall.ware.entity.WareInfoEntity;
import com.example.gulimall.ware.feign.MemberFeignService;
import com.example.gulimall.ware.service.WareInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired private MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");

        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                Wrappers.<WareInfoEntity>lambdaQuery()
                    .like(StringUtils.isNotBlank(key), WareInfoEntity::getName, key)
        );

        return new PageUtils(page);
    }

    @Override
    public FareTo getFare(Long addrId) {
        FareTo fare = new FareTo();

        R memberAddress = this.memberFeignService.getMemberAddress(addrId);
        MemberReceiveAddressEntity address =
                memberAddress.getData("memberReceiveAddress", MemberReceiveAddressEntity.class);
        fare.setAddress(address);

        String fa = address.getPhone().substring(address.getPhone().length() - 1);
        fare.setFare(new BigDecimal(fa));

        return fare;
    }

}